package cn.lix.sc.rpc.impl.common.dto;

import cn.lix.sc.rpc.impl.common.annotations.RpcService;
import cn.lix.sc.rpc.impl.common.constant.ResponseCode;

import java.io.Serializable;
import java.util.Map;

/**
 * 增强的RPC响应对象，支持多种内容类型和流式传输
 * @author lix
 * @date 2024
 */
public class EnhancedRpcResponse implements Serializable {

    private static final long serialVersionUID = -5335485972159292632L;

    private String result;
    private String errorMsg;
    private Integer code;
    private Long cost;
    private String requestId;
    
    // 新增字段
    private RpcService.ContentType contentType = RpcService.ContentType.JSON;
    private Map<String, String> headers;
    private boolean streaming = false;
    
    // 文件相关
    private byte[] fileData;
    private String fileName;
    private String fileContentType;
    private long fileSize;

    public EnhancedRpcResponse() {}

    public static EnhancedRpcResponse success(String result) {
        return new EnhancedRpcResponse(result, null, ResponseCode.SUCCESS, null, null);
    }

    public static EnhancedRpcResponse success(String result, Long cost) {
        return new EnhancedRpcResponse(result, null, ResponseCode.SUCCESS, cost, null);
    }

    public static EnhancedRpcResponse success(String result, Long cost, String requestId) {
        return new EnhancedRpcResponse(result, null, ResponseCode.SUCCESS, cost, requestId);
    }

    public static EnhancedRpcResponse successFile(byte[] fileData, String fileName, String contentType, Long cost, String requestId) {
        EnhancedRpcResponse response = new EnhancedRpcResponse(null, null, ResponseCode.SUCCESS, cost, requestId);
        response.fileData = fileData;
        response.fileName = fileName;
        response.fileContentType = contentType;
        response.fileSize = fileData != null ? fileData.length : 0;
        response.contentType = RpcService.ContentType.OCTET_STREAM;
        return response;
    }

    public static EnhancedRpcResponse error(String errorMsg) {
        return new EnhancedRpcResponse(null, errorMsg, ResponseCode.FAIL, null, null);
    }

    public static EnhancedRpcResponse error(String errorMsg, Long cost) {
        return new EnhancedRpcResponse(null, errorMsg, ResponseCode.FAIL, cost, null);
    }

    public static EnhancedRpcResponse error(String errorMsg, Long cost, String requestId) {
        return new EnhancedRpcResponse(null, errorMsg, ResponseCode.FAIL, cost, requestId);
    }

    private EnhancedRpcResponse(String result, String errorMsg, Integer code, Long cost, String requestId) {
        this.result = result;
        this.errorMsg = errorMsg;
        this.code = code;
        this.cost = cost;
        this.requestId = requestId;
    }

    public boolean ok() {
        return ResponseCode.SUCCESS.equals(this.code);
    }

    public boolean isFile() {
        return fileData != null && fileData.length > 0;
    }

    // Getters and Setters
    public String getResult() {
        return result;
    }

    public void setResult(String result) {
        this.result = result;
    }

    public String getErrorMsg() {
        return errorMsg;
    }

    public void setErrorMsg(String errorMsg) {
        this.errorMsg = errorMsg;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public Long getCost() {
        return cost;
    }

    public void setCost(Long cost) {
        this.cost = cost;
    }

    public String getRequestId() {
        return requestId;
    }

    public void setRequestId(String requestId) {
        this.requestId = requestId;
    }

    public RpcService.ContentType getContentType() {
        return contentType;
    }

    public void setContentType(RpcService.ContentType contentType) {
        this.contentType = contentType;
    }

    public Map<String, String> getHeaders() {
        return headers;
    }

    public void setHeaders(Map<String, String> headers) {
        this.headers = headers;
    }

    public boolean isStreaming() {
        return streaming;
    }

    public void setStreaming(boolean streaming) {
        this.streaming = streaming;
    }

    public byte[] getFileData() {
        return fileData;
    }

    public void setFileData(byte[] fileData) {
        this.fileData = fileData;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public String getFileContentType() {
        return fileContentType;
    }

    public void setFileContentType(String fileContentType) {
        this.fileContentType = fileContentType;
    }

    public long getFileSize() {
        return fileSize;
    }

    public void setFileSize(long fileSize) {
        this.fileSize = fileSize;
    }
} 